package com.qilu.careprogram.controller.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.qilu.careprogram.common.AjaxResult;
import com.qilu.careprogram.pojo.entity.es.QuestionAnswer;
import com.qilu.careprogram.service.ESQuestionAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/es_question_answer")
public class ESQuestionAnswerController {


    @Autowired
    private ElasticsearchClient esClient;
    @Autowired
    private ESQuestionAnswerService esQuestionAnswerService;

    /**
     * 新增
     * @param questionAnswer
     * @return document的id
     */
    @PostMapping("/add")
    public AjaxResult addDocument(@RequestBody QuestionAnswer questionAnswer){

        return AjaxResult.success(esQuestionAnswerService.addDocument(questionAnswer));
    }

    /**
     * 批量插入
     * @param questionAnswerList
     * @return
     */
    @PostMapping("/bulk_add")
    public AjaxResult addBulkDocument(@RequestBody List<QuestionAnswer> questionAnswerList){
        return AjaxResult.success(esQuestionAnswerService.addBulkDocument(questionAnswerList));
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public AjaxResult getAllDocument(@RequestParam Integer page, @RequestParam Integer pageSize){
        return AjaxResult.success(esQuestionAnswerService.getAllDocument(page,pageSize));
    }

    @GetMapping("/condition_query")
    public AjaxResult getByConditionDocument(@RequestParam String condition){

        return AjaxResult.success(esQuestionAnswerService.getByConditionDocument(condition));
    }


    /**
     * 根据id删除document
     * @param id
     * @return
     */
    @DeleteMapping
    public AjaxResult deleteDocument(@RequestParam String id){

        String response = esQuestionAnswerService.deleteDocument(id);
        if(response.equalsIgnoreCase("deleted")){
            return AjaxResult.success("删除成功，ID：" + id);
        }else{
            return AjaxResult.error("删除失败，ID：" + id);
        }


    }


}
